package com.woniuxy01;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author 作者
 * @create 2025-08-04 9:45
 */
public class Test01 {
    public static void main(String[] args) throws IOException {

    }

    private static void testsaveload() throws IOException {
        //        List<Student> students = new ArrayList<Student>();
//        students.add(new Student(1, "zs", 11));
//        students.add(new Student(2, "ls", 12));
//        students.add(new Student(3, "ww", 10));
//        save(students);

        List<Student> students = load();
        System.out.println(students);
    }

    public static List<Student> load() throws IOException {
        //读学生数据文件，将文件中存储的学生信息，还原成对象，存入集合
        BufferedReader br = new BufferedReader(new FileReader("student.txt"));
        String line=null;
        List<Student> students=new ArrayList<>();
        while((line=br.readLine())!=null){
            //读取一行数据，转成学生对象，存入集合
            String[] studentProperies = line.split("-");
            Student student=new Student();
            student.setSid(Integer.parseInt(studentProperies[0]));
            student.setSname(studentProperies[1]);
            student.setAge(Integer.parseInt(studentProperies[2]));
            students.add(student);
        }
        return students;
    }

    public static void save(List<Student> students) throws IOException {
        //将学生集合中的数据存入文件,每个学生信息单独存放一行,属性和属性直接使用-分隔
        //1-zs-11
        //2-ls-12
        BufferedWriter bw = new BufferedWriter(new FileWriter("student.txt"));
        for (Student student : students) {
//            bw.write(student.getSid());
//            bw.write("-");
//            bw.write(student.getSname());
//            bw.write("-");
//            bw.write(student.getAge());
            bw.write(student.getSid()+"-"+student.getSname()+"-"+student.getAge());

            bw.newLine();
            bw.flush();
        }
        bw.close();
    }


    private static void test01() throws IOException {
        //        BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("a.txt")));
        for (int i = 1; i <= 5; i++) {
            bw.write("aaaa" + i);
            //调用当前系统的换行的底层指令
            bw.newLine();
            bw.flush();
        }
        bw.close();

        BufferedReader br = new BufferedReader(new FileReader("a.txt"));
        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
    }
}
